package tt.dz.order.vo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import tt.dz.order.exception.BusinessException;
import tt.dz.order.pojo.OrderChangeInfo;
import tt.dz.order.pojo.OrderInfoChangeLogInfo;
import tt.dz.order.pojo.OrderItemChangeLogInfo;
import tt.dz.order.pojo.OrderItemDividedDetailChangeLogInfo;
import tt.dz.order.util.OrderConstant.OrderStatus;
import tt.dz.order.util.OrderConstant.UserType;
import tt.dz.order.util.page.DefaultPaginationBuilder;
import tt.dz.order.util.page.DefaultPaginationDirector;
import tt.dz.order.util.page.Pagination;
import tt.dz.order.util.page.PaginationDirector;

public class OrderChangeLogVo {
	private static final String select_all_sql = "select changeId,orderNo,userId,userMobileNo,msg,createTime,orderCreateTime from order_change_info where 1=1 ";
	// 订单修改信息
	OrderChangeInfo orderChangeInfo;
	// 订单历史记录
	OrderInfoChangeLogInfo orderInfo;
	// 订单项记录
	List<OrderItemChangeLogInfo> orderItem;
	// 订单项分层记录
	List<OrderItemDividedDetailChangeLogInfo> orderItemDividedDetail;
	static Map<String, String> sqlmap = new HashMap<String, String>();

	static {
		sqlmap.put(QueryOrderChangeLogResportVO.QueryType.userid.value(), " 	AND userId =:queryValue");
		sqlmap.put(QueryOrderChangeLogResportVO.QueryType.orderNo.value(), "	AND orderNo =:queryValue");
		sqlmap.put(QueryOrderChangeLogResportVO.QueryType.userMobileNo.value(), "	AND userMobileNo =:queryValue");
		// sqlmap.put(QueryOrderChangeLogResportVO.QueryType.createTime.value(),
		// " AND createTime =:queryValue");
		// sqlmap.put(QueryOrderChangeLogResportVO.QueryType.orderCreateTime.value(),
		// " AND orderCreateTime =:queryValue");
	}

	/**
	 * 订单修改日志查询
	 * 
	 * @param queryOrderResportVO
	 * @param curPageNo
	 * @param prePageNum
	 * @return
	 * @throws BusinessException
	 */
	public static Pagination<OrderChangeLogVo> queryPagition(QueryOrderChangeLogResportVO queryVO, int curPageNo,
			int prePageNum) throws BusinessException {
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd"); 
		StringBuilder controlBuilder = new StringBuilder();
		if (StringUtils.isEmpty(queryVO.getQueryType()) && !StringUtils.isEmpty(queryVO.getQueryValue())) {
			throw new BusinessException("请选择查询类型");
		}
		if (!StringUtils.isEmpty(queryVO.getQueryType()) && StringUtils.isEmpty(queryVO.getQueryValue())) {
			if (StringUtils.isEmpty(sqlmap.get(queryVO.getQueryType()))) {
				throw new BusinessException("查询类型异常");
			}
			throw new BusinessException("请输入查询内容");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(sqlmap.get(queryVO.getQueryType()))) {
			controlBuilder.append(sqlmap.get(queryVO.getQueryType()));
			paramMap.put("queryValue", queryVO.getQueryValue());
		}
		if (queryVO.getStartDate() != null) {
			controlBuilder.append("		AND  to_char(createtime,'yyyy-MM-dd') >=:startDate");
			paramMap.put("startDate", dateFormat.format(queryVO.getStartDate()));
		}
		if (queryVO.getEndDate() != null) {
			controlBuilder.append("		AND to_char(createtime,'yyyy-MM-dd') <=:endDate");
			paramMap.put("endDate", dateFormat.format(queryVO.getEndDate()));
		}
		;
		String countSql = " select count(1) from order_change_info where 1=1" + controlBuilder.toString();
		PaginationDirector director = new DefaultPaginationDirector(new DefaultPaginationBuilder(), countSql,
				select_all_sql + controlBuilder.append(" ORDER BY createtime DESC ").toString(), paramMap, curPageNo, prePageNum);
		Pagination<OrderChangeInfo> pagination = director.construct(OrderChangeInfo.class);
		List<OrderChangeInfo> list = pagination.getList();
		List<OrderChangeLogVo> changeLogVos = new ArrayList<OrderChangeLogVo>();
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderChangeInfo orderChangeInfo : list) {
				OrderChangeLogVo changeLogVo = new OrderChangeLogVo();
				OrderInfoChangeLogInfo changeLogInfo = OrderInfoChangeLogInfo
						.queryByChangeId(orderChangeInfo.getChangeId());
				changeLogInfo.setStatusName(OrderStatus.getDesc(changeLogInfo.getStatus()));
				changeLogInfo.setUserTypeName(UserType.indexOf(new Integer(changeLogInfo.getUserType())).desc());
				List<OrderItemChangeLogInfo> changeLogInfos = OrderItemChangeLogInfo
						.queryByChangeId(orderChangeInfo.getChangeId());
				List<OrderItemDividedDetailChangeLogInfo> orderItemDividedDetail = OrderItemDividedDetailChangeLogInfo
						.queryByChangeId(orderChangeInfo.getChangeId());
				changeLogVo.setOrderChangeInfo(orderChangeInfo);
				changeLogVo.setOrderInfo(changeLogInfo);
				changeLogVo.setOrderItem(changeLogInfos);
				changeLogVo.setOrderItemDividedDetail(orderItemDividedDetail);
				changeLogVos.add(changeLogVo);
			}
		}
		pagination.setList(list);
		Pagination<OrderChangeLogVo> pagination2 = new Pagination<OrderChangeLogVo>();
		pagination2.setList(changeLogVos);

		pagination2.setAllRow(pagination.getAllRow());
		pagination2.setTotalPage(pagination.getTotalPage());
		pagination2.setCurrentPage(pagination.getCurrentPage());
		pagination2.setPageSize(pagination.getPageSize());

		pagination2.setFirstPage(pagination.isFirstPage());
		pagination2.setLastPage(pagination.isLastPage());
		pagination2.setHasPreviousPage(pagination.isHasPreviousPage());
		pagination2.setHasNextPage(pagination.isHasNextPage());
		return pagination2;
	}

	public OrderChangeInfo getOrderChangeInfo() {
		return orderChangeInfo;
	}

	public void setOrderChangeInfo(OrderChangeInfo orderChangeInfo) {
		this.orderChangeInfo = orderChangeInfo;
	}

	public OrderInfoChangeLogInfo getOrderInfo() {
		return orderInfo;
	}

	public void setOrderInfo(OrderInfoChangeLogInfo orderInfo) {
		this.orderInfo = orderInfo;
	}

	public List<OrderItemChangeLogInfo> getOrderItem() {
		return orderItem;
	}

	public void setOrderItem(List<OrderItemChangeLogInfo> orderItem) {
		this.orderItem = orderItem;
	}

	public static Map<String, String> getSqlmap() {
		return sqlmap;
	}

	public static void setSqlmap(Map<String, String> sqlmap) {
		OrderChangeLogVo.sqlmap = sqlmap;
	}

	public List<OrderItemDividedDetailChangeLogInfo> getOrderItemDividedDetail() {
		return orderItemDividedDetail;
	}

	public void setOrderItemDividedDetail(List<OrderItemDividedDetailChangeLogInfo> orderItemDividedDetail) {
		this.orderItemDividedDetail = orderItemDividedDetail;
	}

}
